package com.cloud.work.job;

import com.cloud.bean.coupon.BatchCoupon;
import com.cloud.bean.coupon.CouponRule;
import com.cloud.bean.rule.Rule;
import com.cloud.service.coupon.BatchCouponService;
import com.cloud.service.coupon.CouponRuleService;
import com.cloud.service.rule.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Stream;

/**
 * @Author liuyuan
 * @Date 2022/8/5 5:42 PM
 * @Version 1.0
 */
@Component
public class TestWorkJob {

    @Autowired
    BatchCouponService batchCouponService;

    @Autowired
    CouponRuleService couponRuleService;

    @Autowired
    RuleService ruleService;




    @Scheduled(cron= "*/30 * * * * ?")
    public void test() {
        //SimpleDateFormat 格式化日期类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //去查询批次表中所有发布优惠卷
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("status","PUSH");
        Collection<BatchCoupon> batchCoupons = batchCouponService.listByMap(queryMap);
        //遍历优惠卷集合
        for (BatchCoupon batchCoupon:batchCoupons) {
           //拿到批次优惠卷的批次号唯一
            String batchCode = batchCoupon.getBatchCode();
            //根据批次号去查询优惠卷规则中间表(批次号关联规则)
            Map<String, Object> queryMap1 = new HashMap<>();
            queryMap1.put("batch_code",batchCode);
            List<Long> ruleIds = new ArrayList<>();
            //获取到优惠的规则
            Collection<CouponRule> couponRules = couponRuleService.listByMap(queryMap1);
            for (CouponRule couponRule: couponRules) {
                //取到所有规则id放入集合中
                ruleIds.add(couponRule.getRuleId());
            }
            //根据所有的规则id去查询所有的规则
            Collection<Rule> rules = ruleService.listByIds(ruleIds);
            Stream<Rule> dates = rules.stream().filter(rule -> rule.getCode().equals("DATE"));
            dates.forEach(ruleDate -> {
                //根据校验类型区分开各个逻辑的执行
                String checkType = ruleDate.getCheckType();
                if(checkType.equals("EXPIRYDATE")){
                    //获取规则中的值
                    String value = ruleDate.getValue();
                    Date parse = null;
                    try {
                        parse = sdf.parse(value);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    long oldTime = parse.getTime();
                    Date nowDate = new Date();
                    //如果当前日期的毫秒值大于规则的毫秒值
                    if(nowDate.getTime()>oldTime){
                        //过期了
                        batchCoupon.setStatus("UNACTIVE");
                        batchCouponService.updateById(batchCoupon);
                    }
                }else if(checkType.equals("VALIDTIME")){

                }else {

                }

            });
        }
    }

    public static void main(String[] args) {



    }
}


